#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vlc/libvlc_media.h>
#include <stdio.h>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    printf("(%s:%d) ctor\n",__FILE__,__LINE__);
    ui->setupUi(this);
    this->ui->horizontalSlider->setMaximum(int(SliderResolution));
    this->poller = new QTimer(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::log(QString msg)
{
    this->ui->textEdit->append(msg);
}

void MainWindow::play()
{
    const char * const vlc_args[] = {
              "-I", "dummy", /* Don't use any interface */
              "--ignore-config", /* Don't use VLC's config */
              "--extraintf=logger", //log anything
              "--verbose=2", //be much more verbose then normal for debugging purpose
              "--plugin-path=C:\\vlc-0.9.9-win32\\plugins\\" };

    _vlcinstance=libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);  //tricky calculation of the char space used
    _mp = libvlc_media_player_new (_vlcinstance);
    //QString file("file:///tmp/big_buck_clip.avi");

    QString file("rtsp://192.168.0.227:8544/video");
    log(QString("playing " + file));
    _m = libvlc_media_new_location (_vlcinstance, file.toAscii());
     libvlc_media_player_set_media (_mp, _m);
     int windid = this->ui->frame->winId();
     libvlc_media_player_set_xwindow (_mp, windid );
     connect(poller, SIGNAL(timeout()), this, SLOT(updateInterface()));
     this->poller->start(100);
     libvlc_media_player_play (_mp );
}

void MainWindow::updateInterface()
{
    float pos=libvlc_media_player_get_position (_mp);
     int siderPos=(int)(pos*SliderResolution);
     this->ui->horizontalSlider->setValue(siderPos);
}
